package com.deyuanyun.pic.pig.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.deyuanyun.pic.cache.SessionCacheSupport2;
import com.deyuanyun.pic.common.exception.PicException;
import com.deyuanyun.pic.common.util.DateUtil;
import com.deyuanyun.pic.controller.dto.UserSessionVO;
import com.deyuanyun.pic.domain.base.Attachment;
import com.deyuanyun.pic.domain.pipe.PipeLine;
import com.deyuanyun.pic.mapper.base.AttachmentMapper;
import com.deyuanyun.pic.mapper.pipe.PipeLineMapper;
import com.deyuanyun.pic.pig.domain.CleanPipeLineManagerVo;
import com.deyuanyun.pic.pig.domain.InspSegment;
import com.deyuanyun.pic.pig.domain.PigTask;
import com.deyuanyun.pic.pig.domain.PigTaskPoint;
import com.deyuanyun.pic.pig.mapper.InspSegmentMapper;
import com.deyuanyun.pic.pig.mapper.PigTaskMapper;
import com.deyuanyun.pic.pig.mapper.PigTaskPointMapper;
import com.deyuanyun.pic.pig.service.CleanPipeLineManagerService;
import com.deyuanyun.pic.support.biz.CodeUtil;

@Service
public class CleanPipeLineManagerServiceImpl implements CleanPipeLineManagerService {
	// 删除标记,1:已删除
	private static final short deleteFlag = 1;
	// 未删除
	private static final short unDeleteFlag = 0;
	// 未发布状态
	private static final short statusFlag1 = 1;
	// 已发布状态
	private static final short statusFlag2 = 2;
	// 清管类型
	private static final String CT = "漏磁检测,超声波检测,几何检测";
	
	private static final short version = 0;
	
	@Resource
	private PigTaskMapper pigTaskMapper;
	@Resource
	private PigTaskPointMapper pigTaskPointMapper;
	@Resource
	private PipeLineMapper pipeLineMapper;
	@Resource
	private InspSegmentMapper inspSegmentMapper;
	@Resource
	private AttachmentMapper attachmentMapper;
	
	@Override
	public List getCleanPipeLineManagerIndex(CleanPipeLineManagerVo taskVo) {
		return pigTaskMapper.getCleanPipeLineManagerIndex(taskVo);
	}

	@Override
	public Map<String, Object> deleteFunById(String idsArr) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = 0;
		String[] idArr = idsArr.split(",");
		StringBuffer sb = new StringBuffer();
		for (String id : idArr) {
			PigTask task = pigTaskMapper.selectByPrimaryKey(id);
			if(null != task){
				if(task.getStaus().equals(statusFlag1)){
					task.setDeleted(deleteFlag);
					pigTaskMapper.updateByPrimaryKeySelective(task);
					count++;
				}
				if(task.getStaus().equals(statusFlag2)){
					sb.append(task.getName()).append(" , ");
				}
			}
		}
		map.put("delCount", count);
		map.put("undelTask", sb.toString());
		return map;
	}

	@Override
	public int publishFunById(String idsArr) throws Exception {
		int count = 0;
		String[] idArr = idsArr.split(",");
		for (String id : idArr) {
			PigTask task = pigTaskMapper.selectByPrimaryKey(id);
			if (task != null) {
				if (task.getStaus().toString().equals("1")) {
					task.setStaus(statusFlag2);
					pigTaskMapper.updateByPrimaryKeySelective(task);
					count++;
				}
			}
		}
		return count;
	}

	@Override
	public List<InspSegment> getSegment(String lineId) {
		return pigTaskMapper.getInspSegment(lineId);
	}

	@Override
	public Map<String, Object> saveTask(Map<String, Object> map, String taskId, String taskName, String lineId,
			String segmentId, String markPoint, String fromPig, String toPig, String remark, String type,
			String pigType, String planPagDateTime,String reverse) throws Exception {
		String[] point = markPoint.split(",");
		UserSessionVO uv = SessionCacheSupport2.getSessionVO();
		Map<String, Object> praM = new HashMap<String, Object>();
		praM.put("taskName", taskName);
		praM.put("taskId", taskId);
		PigTask pt = pigTaskMapper.getPigTaskByName(praM);
		if (null != pt && taskName.equals(pt.getName())) {
			map.put("msgCode", "10");
			return null;
		} else if (null == taskId || "".equals(taskId)) {
			PigTask task = new PigTask();
			task.setName(taskName);
			task.setPipeLineId(lineId);
			task.setStaus(statusFlag1);
			task.setInspSegmentId(segmentId);
			task.setDeleted(unDeleteFlag);
			task.setCreateRemark(remark);
			task.setType(type);
			String type0 = StringUtils.isEmpty(CodeUtil.getName(type)) ? "" : CodeUtil.getName(type);
			if (CT.contains(type0)) {
				task.setDetectorType(pigType);
			} else {
				task.setPigType(pigType);
			}

			task.setCreateUser(uv.getId());
			task.setPlanPagDateTime(DateUtil.convertStringToDate(planPagDateTime));
			pigTaskMapper.insertSelective(task);
			for (String pointId : point) {
				PigTaskPoint tp = new PigTaskPoint();
				tp.setId(UUID.randomUUID().toString().replace("-", ""));
				tp.setDeleted(unDeleteFlag);
				tp.setPiggngTaskId(task.getId());
				tp.setTrackPointId(pointId);
				pigTaskPointMapper.insertSelective(tp);
			}
			// 发球记录表中插入数据【任务，管道，管段，球筒】
			Map<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("id", UUID.randomUUID().toString().replace("-", ""));
			sendMap.put("taskId", task.getId());
			sendMap.put("lineId", task.getPipeLineId());
			sendMap.put("segmentId", task.getInspSegmentId());
			sendMap.put("pigId", fromPig);
			sendMap.put("mark", 0);
			pigTaskMapper.insertBall(sendMap);
			// 收球记录表中插入数据【任务，管道，管段，球筒】
			Map<String, Object> backMap = new HashMap<String, Object>();
			backMap.put("id", UUID.randomUUID().toString().replace("-", ""));
			backMap.put("taskId", task.getId());
			backMap.put("lineId", task.getPipeLineId());
			backMap.put("segmentId", task.getInspSegmentId());
			backMap.put("pigId", toPig);
			backMap.put("mark", 1);
			pigTaskMapper.insertBall(backMap);
			map.put("msgCode", "done");
			map.put("tid", task.getId());
			
			//中间易位表插入数据
			Map<String, Object> reverseMap = new HashMap<String, Object>();
			reverseMap.put("id", UUID.randomUUID().toString().replace("-", ""));
			reverseMap.put("taskId", task.getId());
			reverseMap.put("lineId", task.getPipeLineId());
			reverseMap.put("segmentId", task.getInspSegmentId());
			reverseMap.put("fromPig", fromPig);
			reverseMap.put("toPig", toPig);
			reverseMap.put("reverse", reverse);
			pigTaskMapper.insertReverse(reverseMap);
		} else {
			PigTask task = pigTaskMapper.selectByPrimaryKey(taskId);
			task.setName(taskName);
			task.setPipeLineId(lineId);
			task.setUpdateDate(DateUtil.getCurrentDate());
			task.setCreateRemark(remark);
			task.setInspSegmentId(segmentId);
			task.setType(type);
			String type0 = StringUtils.isEmpty(CodeUtil.getName(type)) ? "" : CodeUtil.getName(type);
			if (CT.contains(type0)) {
				task.setPigType("");
				task.setDetectorType(pigType);
			} else {
				task.setPigType(pigType);
				task.setDetectorType("");
			}
			task.setUpdateUser(uv.getId());
			task.setPlanPagDateTime(DateUtil.convertStringToDate(planPagDateTime));
			pigTaskMapper.updateByPrimaryKeySelective(task);
			// 查询该任务对应检测管段下的跟踪点
			List<PigTaskPoint> tpList = pigTaskMapper.getOldPointByTaskId(taskId);
			if (tpList.size() > 0) {
				for (PigTaskPoint taskPoint : tpList) {
					PigTaskPoint tp = pigTaskPointMapper.selectByPrimaryKey(taskPoint.getId());
					// 删除跟踪点
					tp.setDeleted(deleteFlag);
					pigTaskPointMapper.updateByPrimaryKeySelective(tp);
				}
			}
			// 重新创建跟踪点
			for (String pointId : point) {
				PigTaskPoint tp = new PigTaskPoint();
				tp.setId(UUID.randomUUID().toString().replace("-", ""));
				tp.setDeleted(unDeleteFlag);
				tp.setPiggngTaskId(task.getId());
				tp.setTrackPointId(pointId);
				pigTaskPointMapper.insertSelective(tp);
			}
			
			Map<String,Object> reverseMap = new HashMap<String, Object>();
			reverseMap.put("segmentId", null);
			reverseMap.put("taskId", task.getId());
			// 更新发球记录表
			Map<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("taskId", taskId);
			sendMap.put("lineId", task.getPipeLineId());
			sendMap.put("segmentId", task.getInspSegmentId());
			sendMap.put("pigId", pigTaskMapper.getPigByInspSegmentId(reverseMap).getFromPig());
			sendMap.put("mark", 0);
			pigTaskMapper.updateBall(sendMap);
			// 更新收球记录表
			Map<String, Object> backMap = new HashMap<String, Object>();
			backMap.put("taskId", taskId);
			backMap.put("lineId", task.getPipeLineId());
			backMap.put("segmentId", task.getInspSegmentId());
			backMap.put("pigId", pigTaskMapper.getPigByInspSegmentId(reverseMap).getToPig());
			backMap.put("mark", 1);
			pigTaskMapper.updateBall(backMap);
			map.put("msgCode", "done");
			map.put("tid", task.getId());
			//先删除易位表数据
			pigTaskMapper.deleteReverseByTaskId(taskId);
			//中间易位表插入数据
			Map<String, Object> reverseDataMap = new HashMap<String, Object>();
			reverseDataMap.put("id", UUID.randomUUID().toString().replace("-", ""));
			reverseDataMap.put("taskId", task.getId());
			reverseDataMap.put("lineId", task.getPipeLineId());
			reverseDataMap.put("segmentId", task.getInspSegmentId());
			reverseDataMap.put("fromPig", fromPig);
			reverseDataMap.put("toPig", toPig);
			reverseDataMap.put("reverse", reverse);
			pigTaskMapper.insertReverse(reverseDataMap);
			
		}
		return map;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map<String, Object> getMarkPoint(Map<String, Object> map, String inspSegmentId) {
		// List<Map<String,Object>> list = new ArrayList<>();
		List list = pigTaskMapper.getMarkPoint(inspSegmentId);
		/*
		 * for (Map<String,Object> m : list) {
		 * m.put("TYPE",CodeUtil.getName(m.get("TYPE").toString())); }
		 */
		map.put("markPoint", list);
		// 根据inspsegmentId获取收发球桶
		map.put("segmentId",inspSegmentId);
		map.put("taskId",null);
		CleanPipeLineManagerVo p = pigTaskMapper.getPigByInspSegmentId(map);
		map.put("pigging", p);
		return map;
	}

	@Override
	public ModelAndView getData(ModelAndView mv, String taskId) throws Exception {
		List list = new ArrayList<>();
		try {
			PigTask task = pigTaskMapper.selectByPrimaryKey(taskId);
			if (task != null) {
				// 获取任务
				mv.addObject("modTask", task);

				// 该检测任务对应的管道
				PipeLine pl = pipeLineMapper.selectByPrimaryKey(task.getPipeLineId());
				mv.addObject("modPipeLine", pl);
				// 所有管道
				List<PipeLine> allLineList = pipeLineMapper.getLine();
				mv.addObject("modAllLine", allLineList);

				// 该清管任务对应的管段
				CleanPipeLineManagerVo ins = pigTaskMapper.getInspSegmentByTaskId(task.getId());
				mv.addObject("modInspSegment", ins);
				// 该任务对应管道的所有管段
				List<InspSegment> allSegmentList = pigTaskMapper.getAllInspSegmentByTaskId(task.getId());
				mv.addObject("modAllInspSegment", allSegmentList);

				// 该检测任务对应的跟踪点通过记录
				List<String> markPoint = pigTaskMapper.getInspTaskPoint(task.getId());
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < markPoint.size(); i++) {
					if (i == markPoint.size() - 1) {
						sb.append(markPoint.get(i));
					} else {
						sb.append(markPoint.get(i)).append(",");
					}
				}
				mv.addObject("modPoint", sb.toString());
				// 该检测任务对应检测管段的所有跟踪点
				List allPoint = pigTaskMapper.getMarkPoint(task.getInspSegmentId());
				mv.addObject("modAllPoint", allPoint);
			}
			// list = pigTaskMapper.getPointByTaskId(taskId);
			// mv.addObject("points", list);
		} catch (PicException e) {
			e.printStackTrace();
		}
		return mv;
	}

	@Override
	public PigTask getTask(String taskId) {
		PigTask ts = pigTaskMapper.selectByPrimaryKey(taskId);
		return ts;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map<String, Object> addOrModify(String taskId, Map<String, Object> map) {
		PigTask task = null;
		// 清管类型
		List<CleanPipeLineManagerVo> clist = pigTaskMapper.getcleanType();
		// 用来检验清管工具是下拉选择的的还是输入的
		boolean flag = false;

		if (null == taskId || "".equals(taskId)) {
			map.put("title", "新建任务");
		} else {
			map.put("title", "修改任务");
		}
		task = getTask(taskId);
		// 所有管道
		List<PipeLine> allLineList = pipeLineMapper.getLine();
		map.put("allPipleLine", allLineList);
		if (null != task) {
			// 该检测任务对应的管道
			PipeLine pl = pipeLineMapper.selectByPrimaryKey(task.getPipeLineId());
			map.put("pipleLine", pl);

			// 该清管任务对应的管段
			CleanPipeLineManagerVo ins = pigTaskMapper.getInspSegmentByTaskId(task.getId());
			map.put("segment", ins);
			// 该任务对应管道的所有管段
			List allSegmentList = pigTaskMapper.getAllInspSegmentByTaskId(task.getId());
			map.put("allSegment", allSegmentList);

			// 该任务对应的跟踪点通过记录
			List<String> markPoint = pigTaskMapper.getInspTaskPoint(task.getId());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < markPoint.size(); i++) {
				if (i == markPoint.size() - 1) {
					sb.append(markPoint.get(i));
				} else {
					sb.append(markPoint.get(i)).append(",");
				}
			}
			map.put("point", sb.toString());
			// 该任务对应检测管段的所有跟踪点 要从易位表中进行查询
			String reverse = pigTaskMapper.selectReverseByTaskId(task.getId());
			if(!StringUtils.isEmpty(reverse)){
				Map<String,Object> reMap = new HashMap<String,Object>();
				reMap.put("segmentId", task.getInspSegmentId());
				reMap.put("reverse", reverse);
				List allPoint = pigTaskMapper.getReverseMarkPoint(reMap);
				map.put("allPoint", allPoint);
			}

			// 该任务的清管类型
			String cleanTypeId = task.getType();
			CleanPipeLineManagerVo cleanTypeVo = pigTaskMapper.getcleanTypeByTypeId(cleanTypeId);
			String cleanType = cleanTypeVo.getName();
			map.put("cleanTypeMod", cleanTypeVo);
			// 所有清管类型
			if (clist != null) {
				map.put("cleanType", clist);
			}

			// 该任务的清管工具
			String cleanTool = "";
			String cleanToolId = "";
			// 所有清管工具
			String ct[] = CT.split(",");
			for (int i = 0; i < ct.length; i++) {
				if (cleanType.equals(ct[i])) {
					List ls = pigTaskMapper.getCleanTool();
					if (ls.size() > 0) {
						map.put("cleanToolAll", ls);
						flag = true;
					}
					cleanToolId = task.getDetectorType();
					cleanTool = CodeUtil.getName(cleanToolId);
				}
			}
			if (!flag) {
				cleanTool = task.getPigType();
			}
			map.put("cleanTool", cleanTool);
			map.put("cleanToolId", cleanToolId);
			// 收发球桶
			map.put("segmentId",null);
			map.put("taskId",task.getId());
			CleanPipeLineManagerVo pig = pigTaskMapper.getPigByInspSegmentId(map);
			map.put("pig", pig);
			
			// 备注
			map.put("remark", task.getCreateRemark());
			//附件
			List<Attachment> attachList = pigTaskMapper.selectAttachmentByTaskId(task.getId());
			map.put("attachList", attachList);
			map.put("taskName", task.getName());
			map.put("task", task);
			map.put("reverse", reverse);
		} else {
			map.put("taskName", "");
			// 新增时
			if (allLineList.size() > 0) {
				// 设置默认管道
				PipeLine pline = allLineList.get(0);
				map.put("pipleLine", pline);
				List sList = pigTaskMapper.getInspSegment(pline.getId());
				if (sList.size() > 0) {
					// 设置默认管段
					CleanPipeLineManagerVo iseg = (CleanPipeLineManagerVo) sList.get(0);
					map.put("segment", iseg);
					// 获取收发球点
					map.put("segmentId",iseg.getId());
					map.put("taskId",null);
					CleanPipeLineManagerVo pig = pigTaskMapper.getPigByInspSegmentId(map);
					map.put("pig", pig);
					// 该检测管段下的所有跟踪点
					List allPoint = pigTaskMapper.getMarkPoint(iseg.getId());
					map.put("allPoint", allPoint);

				}
				map.put("allSegment", sList);
				if (clist != null) {
					// 获取清管类型
					map.put("cleanType", clist);
					/*
					 * //设置默认清管类型 CleanPipeLineManagerVo cv = clist.get(0);
					 * pram.addObject("cleanTypeDefault",
					 * cv.getName().toString());
					 */
				}
			}
			flag = true;
		}
		map.put("flag", flag);
		map.put("taskId", taskId);
		return map;
	}

	@Override
	public Map<String, Object> getCleanTool(String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ct[] = CT.split(",");
		boolean mark = false;
		for (int i = 0; i < ct.length; i++) {
			if (name.equals(ct[i])) {
				List ls = pigTaskMapper.getCleanTool();
				if (ls.size() > 0) {
					map.put("cleanTool", ls);
					mark = true;
				}
			}
		}
		map.put("mark", mark);
		return map;
	}

	@Override
	public Map<String, Object> copyTask(String taskId) throws Exception {
		boolean f = false;
		Map<String, Object> map = new HashMap<String, Object>();
		PigTask pt = pigTaskMapper.selectByPrimaryKey(taskId);
		/*
		 * if(!pt.getStaus().equals(statusFlag1)){ map.put("copyCode", "1");
		 * }else{
		 */
		// 该任务的清管工具
		String ct = "";
		String ctId = "";
		// 需要判断的清管类型
		String cta[] = CT.split(",");
		// 所有清管工具
		List ls = pigTaskMapper.getCleanTool();
		map.put("cta", ls);
		// 清管类型
		CleanPipeLineManagerVo type = pigTaskMapper.getcleanTypeByTypeId(pt.getType());
		for (int i = 0; i < cta.length; i++) {
			if (type.getName().equals(cta[i])) {
				/*
				 * if(ls.size() > 0){ f = true; }
				 */
				f = true;
				ctId = pt.getDetectorType();
				ct = CodeUtil.getName(ctId);
			}
		}
		if (!f) {
			ct = pt.getPigType();
		}
		map.put("ct", ct);
		map.put("ctId", ctId);
		PipeLine pl = pipeLineMapper.selectByPrimaryKey(pt.getPipeLineId());
		InspSegment ps = inspSegmentMapper.selectByPrimaryKey(pt.getInspSegmentId());
		// 收发球桶
		map.put("segmentId",null);
		map.put("taskId",taskId);
		CleanPipeLineManagerVo pigg = pigTaskMapper.getPigByInspSegmentId(map);
		map.put("pt", pt);
		map.put("pl", pl);
		map.put("ps", ps);
		map.put("f", f);
		map.put("type", type);
		map.put("pigg", pigg);
		// }
		return map;
	}

	@Override
	public Map<String, Object> copySave(String taskId, String cleanTool, String remark, String taskName,
			String planTime,String fromPig, String toPig) throws Exception {
		UserSessionVO uv = SessionCacheSupport2.getSessionVO();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> praM = new HashMap<String, Object>();
		praM.put("taskName", taskName);
		praM.put("taskId", "");
		// 任务名称验证
		PigTask pt = pigTaskMapper.getPigTaskByName(praM);
		if (null != pt && taskName.equals(pt.getName())) {
			map.put("msgCode", "1");
		} else {
			// 复制任务
			PigTask target = new PigTask();
			PigTask source = pigTaskMapper.selectByPrimaryKey(taskId);
			target.setCreateRemark(remark);
			target.setCreateDate(DateUtil.getCurrentDate());
			target.setDeleted(unDeleteFlag);
			target.setInspSegmentId(source.getInspSegmentId());
			target.setName(taskName);
			target.setType(source.getType());
			String type0 = StringUtils.isEmpty(CodeUtil.getName(source.getType())) ? ""
					: CodeUtil.getName(source.getType());
			if (CT.contains(type0)) {
				target.setDetectorType(cleanTool);
			} else {
				target.setPigType(cleanTool);
			}
			target.setPipeLineId(source.getPipeLineId());
			target.setPlanPagDateTime(DateUtil.convertStringToDate(planTime+":00"));
			target.setStaus(statusFlag1);
			target.setType(source.getType());
			target.setCreateUser(uv.getId());
			// BeanUtils.copyProperties(source, target, new String[] {
			// "pigType","createRemark","updateDate","staus","createDate",
			// "updateUser","pagDateTime","detectorType" });
			pigTaskMapper.insertSelective(target);
			// 复制跟踪点
			List<PigTaskPoint> list = pigTaskMapper.getOldPointByTaskId(taskId);
			for (PigTaskPoint pigTaskPoint : list) {
				PigTaskPoint oldP = pigTaskPointMapper.selectByPrimaryKey(pigTaskPoint.getId());
				PigTaskPoint newP = new PigTaskPoint();
				newP.setCreateDate(DateUtil.getCurrentDate());
				newP.setDeleted(unDeleteFlag);
				newP.setPiggngTaskId(target.getId());
				newP.setTrackPointId(oldP.getTrackPointId());
				newP.setCreateUser(uv.getId());
				// BeanUtils.copyProperties(oldP, newP, new
				// String[]{"createRemark,updateDate,createDate,recrder,passTime,updateUser"});
				pigTaskPointMapper.insertSelective(newP);
			}
			
			// 发球记录表中插入数据【任务，管道，管段，球筒】
			Map<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("id", UUID.randomUUID().toString().replace("-", ""));
			sendMap.put("taskId", target.getId());
			sendMap.put("lineId", target.getPipeLineId());
			sendMap.put("segmentId", target.getInspSegmentId());
			sendMap.put("pigId", fromPig);
			sendMap.put("mark", 0);
			pigTaskMapper.insertBall(sendMap);
			// 收球记录表中插入数据【任务，管道，管段，球筒】
			Map<String, Object> backMap = new HashMap<String, Object>();
			backMap.put("id", UUID.randomUUID().toString().replace("-", ""));
			backMap.put("taskId", target.getId());
			backMap.put("lineId", target.getPipeLineId());
			backMap.put("segmentId", target.getInspSegmentId());
			backMap.put("pigId", toPig);
			backMap.put("mark", 1);
			pigTaskMapper.insertBall(backMap);
			//复制易位表
			Map<String, Object> reMap = new HashMap<String, Object>();
			reMap.put("id", UUID.randomUUID().toString().replace("-", ""));
			reMap.put("taskId", target.getId());
			reMap.put("lineId", target.getPipeLineId());
			reMap.put("segmentId", target.getInspSegmentId());
			reMap.put("fromPig", fromPig);
			reMap.put("toPig", toPig);
			String reverse = pigTaskMapper.selectReverseByTaskId(source.getId());
			reMap.put("reverse", reverse);
			pigTaskMapper.insertReverse(reMap);
			//任务附件
			List<Attachment> attList = pigTaskMapper.selectAttachmentByTaskId(source.getId());
			if(attList.size() > 0){
				for (Attachment attachment : attList) {
					Attachment at = new Attachment();
					at.setId(UUID.randomUUID().toString().replace("-", ""));
					at.setPath(attachment.getPath());
					at.setRealName(attachment.getRealName());
					at.setBusinessId(target.getId());
					at.setFileType(attachment.getFileType());
					at.setSource(attachment.getSource());
					at.setDeleted(unDeleteFlag);
					attachmentMapper.insertSelective(at);
				}
			}
			map.put("msgCode", "ok");
		}
		return map;
	}

	@Override
	public void savefile(String sourceName, String targetName ,String tid) throws Exception{
		List<Attachment> attList = pigTaskMapper.selectAttachmentByTaskId(tid);
		if(attList.size() > 0){
			for (Attachment at : attList) {
				Attachment ach = attachmentMapper.selectByPrimaryKey(at.getId());
				ach.setDeleted(deleteFlag);
				attachmentMapper.updateByPrimaryKeySelective(ach);
			}
		}
		
		Attachment ach = new Attachment();
		String attaid = UUID.randomUUID().toString().replace("-", "");
		ach.setId(attaid);
		ach.setBusinessId(tid);
		ach.setDeleted(unDeleteFlag);
		ach.setVersion(version);
		ach.setBusinessId(tid);
		ach.setFileType("OTHER");
		ach.setRealName(sourceName);
		ach.setPath(targetName);
		ach.setSource("PIC_PIG_TASK");
		attachmentMapper.insertSelective(ach);
		return ;
	}

	@Override
	public List<Attachment> downloadTaskFile(String taskId) {
		return pigTaskMapper.selectAttachmentByTaskId(taskId);
	}

	@Override
	public List findAllMapData(Map<String, Object> map) {
		return pigTaskMapper.findAllMapData(map);
	}

	@Override
	public List<String> findAllSegmentByInspSegmentId(String segmentId) {
		return pigTaskMapper.findAllSegmentByInspSegmentId(segmentId);
	}

	@Override
	public List<HashMap<String, Object>> findZuoBiaoByInspSegmentId(Map<String, Object> map) {
		return pigTaskMapper.findZuoBiaoByInspSegmentId(map);
	}
}
